/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com, guwei8576431@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE 
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 * 
 * Created on 2009-10-23
 *******************************************************************************/

package org.cgtw.scriptprofile.server.manager;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.FileUtils;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponseInfo;

public class ResourceManager {
	private static ResourceManager instance = null;
	private String tempPath = null;
	
	private ResourceManager(){
		init();
	}
	
	public static ResourceManager getInstance(){
		if(instance == null){
			instance = new ResourceManager();
		}
		return instance;
	}
	
	//create profile temp dir if not exists
	private void init(){
		String scriptProfileHome = System.getProperty("scriptProfile.home");
		File file = new File(scriptProfileHome, "scriptProfileTemp");
		if(!file.exists()){
			file.mkdirs();
		}
		this.tempPath = file.getAbsolutePath();
	}
	
	public File getTempFolder() {
		File folder = new File(this.tempPath);
		return folder.getAbsoluteFile();
	}
	
	public String createResourceFile(String path, ScriptProfileResponseInfo info){
		return createResourceFile(path, info.getInputStream(), info.getEncoding());
	}
	
	public String createResourceFile(String path, InputStream stream, String encoding){
		String realPath = getResourceRealPath(path);
		createParentFolder(realPath);
		if("gzip".equals(encoding)){
			writeGzip2File(new File(realPath),stream);
			return realPath;
		}

		try {
			OutputStream out = new FileOutputStream(realPath);
			byte buf[] = new byte[1024];
			int len;
			while ((len = stream.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.close();
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return realPath;
	}
	
	public String getResourceRealPath(String resource) {
		if (resource.endsWith("/")) {
			resource = resource + "_homePage";
		}
		File file = new File(getTempFolder(), resource);
		return file.getAbsolutePath().replace('\\', '/');
	}

	private void writeGzip2File(File file, InputStream inputStream){
		ByteArrayOutputStream out = new ByteArrayOutputStream();
        // Transfer bytes from the compressed stream to the output stream
        byte[] buf;
		try {
			inputStream = new GZIPInputStream(inputStream);
			buf = new byte[1024];
			int len;
			while ((len = inputStream.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			FileUtils.writeByteArrayToFile(file, out.toByteArray());
			// Close the file and stream
			inputStream.close();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void createParentFolder(String realPath) {
		File parent = new File(realPath).getParentFile();
		if (!parent.exists()) {
			parent.mkdirs();
		}
	}

}
